package com.mflex.mwms.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.mwms.R;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.ShelfOnLocation;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OBACheckShelfOffActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.stv_Pkg)
    EditText stvPkg;

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.stv_ToShelfLocation)
    EditText stvToShelfLocation;

    @BindView(R.id.stv_ShelfLocation)
    EditText stvShelfLocation;

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

    MiniLoadingDialog mMiniLoadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oba_check_shelf_off_activity);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stvBoxNumber.setInputType(InputType.TYPE_NULL);
        stvToShelfLocation.setInputType(InputType.TYPE_NULL);

        stvPkg.setOnKeyListener(this);
        stvShelfLocation.setOnKeyListener(this);

        lvTaskList.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_BoxNumber:
                    getTask(view);
                    break;
                case R.id.stv_ShelfLocation:
//                    doSubmit(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void getTask(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/ShelfOn/CheckShelfOff")
                .params("BoxNumber", stvPkg.getText().toString())
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .params("checkAction", 3)
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<ShelfOnLocation>, ShelfOnLocation>(new SimpleCallBack<ShelfOnLocation>() {

                    @Override
                    public void onSuccess(ShelfOnLocation response) {
                        mMiniLoadingDialog.dismiss();

                        stvBoxNumber.setText(response.getBoxNumber());
                        stvToShelfLocation.setText(response.getCurrentLocation());

                        stvShelfLocation.requestFocus();
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvPkg.setText("");
                        stvPkg.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(OBACheckShelfOffActivity.this, e);
                    }
                }) {
                });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}